package com.dhcc.bpm.modules.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.bpm.modules.system.entity.UserRole;
import com.dhcc.bpm.modules.system.mapper.UserRoleMapper;
import com.dhcc.bpm.modules.system.service.IUserRoleService;
import com.dhcc.bpm.modules.system.vo.UserRoleVO;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserRoleServiceImpl
 * @Description 服务实现类
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    /**
     * 自定义分页
     * @Author sjm
     * @Date 19:21 2020/2/21 0021
     * @param page
     * @param userRole
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.system.vo.UserRoleVO>
     **/
	@Override
	public IPage<UserRoleVO> selectUserRolePage(IPage<UserRoleVO> page, UserRoleVO userRole) {
		return page.setRecords(baseMapper.selectUserRolePage(page, userRole));
	}

	/**
	 * 添加用户角色中间表
	 * @Author sjm
	 * @Date 19:21 2020/2/21 0021
	 * @param userId
	 * @param roleId
	 * @return boolean
	 **/
	@Override
	public boolean batchUserRole(Long userId, String roleId) {
        List<Long> roleIds = Func.toLongList(roleId);
        ArrayList<UserRole> list = new ArrayList<>();
        roleIds.forEach(role->{
            UserRole userRole = new UserRole();
            userRole.setRoleId(role);
            userRole.setUserId(userId);
            list.add(userRole);
        });
        return saveBatch(list);

    }

    /**
     * 查询集合
     * @Author sjm
     * @Date 19:22 2020/2/21 0021
     * @param userId
     * @return java.util.List<com.dhcc.bpm.modules.system.entity.UserRole>
     **/
    @Override
    public List<UserRole> selectUserByUserId(Long userId) {
        return baseMapper.selectUserByUserId(userId);
    }

    /**
     * 根据userId删除
     * @Author sjm
     * @Date 19:22 2020/2/21 0021
     * @param userId
     * @return boolean
     **/
    @Override
    public boolean removeByUserId(Long userId) {
        return baseMapper.removeByUserId(userId);
    }

}
